<template>
  <div class="home-container">
    <!-- 侧边栏导航 -->
    <el-aside class="sidebar" width="200px">
      <el-menu
        :default-active="activeMenu"
        class="sidebar-menu"
        background-color="#545c64"
        text-color="#fff"
        active-text-color="#ffd04b"
        :collapse="isCollapse"
        router
        @select="handleMenuSelect"
      >
        <div class="logo-container">
          <img
            @click="router.push('/Index')"
            src="../../assets/logo.png"
            alt=""
            v-if="!isCollapse"
            style="width: 180px; height: 100px"
          />
        </div>

        <template v-for="item in menuStore.menuItems" :key="item.menuid">
          <!-- 一级菜单 -->
          <el-sub-menu
            v-if="item.children && item.children.length > 0"
            :index="item.route || item.menuname"
          >
            <template #title>
              <el-icon v-if="item.icon">
                <component :is="item.icon" />
              </el-icon>
              <span>{{ item.menuname }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              v-for="subItem in item.children"
              :key="subItem.menuid"
              :index="subItem.route"
            >
              <el-icon v-if="subItem.icon">
                <component :is="subItem.icon" />
              </el-icon>
              <template #title>{{ subItem.menuname }}</template>
            </el-menu-item>
          </el-sub-menu>

          <!-- 单独菜单项 -->
          <el-menu-item v-else :index="item.route">
            <el-icon v-if="item.icon">
              <component :is="item.icon" />
            </el-icon>
            <template #title>{{ item.menuname }}</template>
          </el-menu-item>
        </template>
      </el-menu>
    </el-aside>

    <!-- 主内容区域 -->
    <el-container>
      <!-- 头部 -->
      <el-header class="header">
        <div class="header-left">
          <el-button @click="toggleCollapse" class="collapse-button" circle>
            <el-icon>
              <Expand v-if="isCollapse" />
              <Fold v-else />
            </el-icon>
          </el-button>
        </div>

        <div class="header-right">
          <!-- 用户信息 -->
          <el-dropdown @command="handleUserCommand">
            <div class="user-info">
              <el-avatar
                :src="userStore.getImgUrl || defaultAvatar"
                size="small"
                class="user-avatar"
              />
              <span class="username">{{ userStore.getUserName }}</span>
            </div>

            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">个人中心</el-dropdown-item>
                <el-dropdown-item command="settings">设置</el-dropdown-item>
                <el-dropdown-item divided command="logout"
                  >退出登录</el-dropdown-item
                >
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <!-- 主体内容 -->
      <el-main class="main-content">
        <router-view />
      </el-main>
    </el-container>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { useRoute, useRouter } from "vue-router";
import { useLoginStore } from "@/stores/userStores";
import { ElMessageBox, ElMessage } from "element-plus";
// 使用存储和路由
const userStore = useLoginStore();
const route = useRoute();
const router = useRouter();

const menuStore = reactive({
  menuItems: [],
});

// 响应式数据
const isCollapse = ref(false);
const activeMenu = ref("");

// 切换侧边栏折叠状态
const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value;
};

// 处理菜单选择
const handleMenuSelect = (index) => {
  activeMenu.value = index;
};

// 用户下拉菜单处理
const handleUserCommand = (command) => {
  switch (command) {
    case "profile":
      router.push("/profile");
      break;
    case "settings":
      router.push("/settings");
      break;
    case "logout":
      handleLogout();
      break;
  }
};

// 退出登录
const handleLogout = () => {
  ElMessageBox.confirm("确定要退出登录吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      // 清除用户信息
      userStore.logout();
      // 跳转到登录页
      router.push("/login");
      ElMessage.success("已退出登录");
    })
    .catch(() => {
      // 用户取消操作
    });
};

// 初始化
onMounted(() => {
  menuStore.menuItems = userStore.getMenus;
  activeMenu.value = route.path;
});
</script>

<style scoped>
.home-container {
  height: 100vh;
  display: flex;
}

.sidebar {
  height: 100%;
  transition: width 0.3s;
}

.sidebar-menu {
  height: 100%;
  border-right: none;
}

.logo-container {
  text-align: center;
  color: white;
  padding: 16px 0;
  border-bottom: 1px solid #434b54;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  background-color: #ffffff;
  border-bottom: 1px solid #e6e6e6;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
}

.collapse-button {
  margin-right: 15px;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.user-avatar {
  margin-right: 10px;
}

.username {
  font-size: 14px;
  color: #333;
}

.main-content {
  padding: 20px;
  background-color: #f0f2f5;
  height: calc(100vh - 60px);
  overflow-y: auto;
}

.el-main {
  --el-main-padding: 0px !important;
}
</style>
